import React from 'react'
import { BaseListPage, IBaseListPageProps, IBaseListPageState, columnRenders, validators } from '../../../lib'
import { Button, Select } from 'antd'
import apis from '@/apis/ContractManage'
import moment from 'moment'
import { uaaApp, http } from '@/lib'
import udMainStore from '@/store/UdMain'
import UdSelect from '@/components/UdFormControls/UdSelect'
import { getBrandSelectList, getDealerListByBrand } from '@/common/Request'

interface IPurchaseListProps extends IBaseListPageProps { }
interface IPurchaseListState extends IBaseListPageState {
  brandOptions: any[]
  dealerOptions: any[]
 }

//接口字段映射
const fieldMap = {
  //合同编号
  no: "Number",
  //合同名称
  name: "Name",
  //甲方公司名称
  componyName: "PartyNameA",
  //状态
  status: "ContractStatusName",
  //有效日期
  time: "receivedTime",
  //开始时间
  BeginTime: "BeginTime",
  //结束时间
  EndTime: "EndTime",

  //状态 - 生效中
  isGoing: "IN_EFFECT",
  //已读 - 已终止
  haveEnd: "TERMINATED"

}


export default class List extends BaseListPage<IPurchaseListProps, IPurchaseListState> {

  protected saveParamsWithUrl: boolean = true

  constructor(props: IPurchaseListProps) {
    super(props)

    this.state = {
      title: '经销商合同',
      queryApi: (data) => http.post(apis.GetContractList, { ...data, BrandCodes: [data.BrandCodes] }),
      method: "POST",
      conditions: [
        { id: "BrandCodes", label: '销售组织', rules: [validators.required], render: () => {
          return (
            <UdSelect onChange={this.getDealerListByBrand} allowClear={false} options={this.state.brandOptions} />
          )
        } },
        { id: "dealerCode", label: '经销商', rules: [validators.required], render: () => {
          return (
            <UdSelect options={this.state.dealerOptions} allowClear={false} showSearch={true} />
          )
        } },
        { id: "NumberOrName", label: '合同编号或名称' },
        {
          id: 'ContractStatus', label: "状态", render: <Select>
            <Select.Option value={fieldMap.isGoing}>生效中</Select.Option>
            <Select.Option value={fieldMap.haveEnd}>已终止</Select.Option>
          </Select>
        },
      ],
      rowKey: (record, index) => index + "",
      columns: [
        ['合同编号', fieldMap.no],
        ['合同名称', fieldMap.name],
        ["甲方公司名称", fieldMap.componyName],
        {
          dataIndex: fieldMap.status, title: "状态",
        },
        { dataIndex: fieldMap.time, title: "有效日期", render: (text, record, index) => `${record[fieldMap.BeginTime] ? moment(record[fieldMap.BeginTime]).format("YY-MM-DD") : ""} 至 ${record[fieldMap.EndTime] ? moment(record[fieldMap.EndTime]).format("YY-MM-DD") : ""}` },
        { dataIndex: "dos", title: "操作", width: 90, render: columnRenders.operate([
          { title: '查看详情', action: (text, record) => this.handleDetails(record.Number) }
        ]) }
      ],
      brandOptions: [],
      dealerOptions: [],
    }
  }

  componentDidMount() {
    this.getBrandList()
    const { BrandCodes, dealerCode } = this.queryParams.conditions
    if (BrandCodes && dealerCode) {
      this.getDealerListByBrand(BrandCodes)
      super.componentDidMount()
    }
  }

  //查看详情
  private handleDetails = (id) => {
    const { dealerCode } = this.queryParams.conditions
    this.props.history.push({
      pathname: `/contract-management/details/${id}`,
      search: `dealerCode=${dealerCode}`
    })
  }
  
  private getBrandList = () => {
    getBrandSelectList().then((result: any) => {
      this.setState({
        brandOptions: result,
      })
    })
  }

  private getDealerListByBrand = (code) => {
    // 组织切换，重置经销商条件
    this.filterForm.setFieldsValue({ dealerCode: undefined })
    getDealerListByBrand(code).then((result: any) => {
      this.setState({
        dealerOptions: result,
      })
    })
  }

  protected filterReset = () => {
    this.setState({
      dataSource: [],
    })
  }

}

